<?php

namespace Drupal\shushengyuan\Schemas;

use Drupal\shusy_category\Entity\ShusyCategory;
use Drupal\shusy_index_category\Entity\ShusyIndexCategory;

/**
 * @OA\Schema(
 *   schema="CategoryIndexItem",
 *   type="object",
 *   description="CategoryIndexItem"
 * )
 */
class CategoryIndexItem extends AbstractBaseModel {

  protected function getEntityType() {
    return 'shusy_index_category';
  }

  protected function setValue() {
    if ($this->entity instanceof ShusyIndexCategory) {
      $query = $this->getQuery($this->entity);
      $url = "/pages/index/goodslist/goodslist?op=query&query={$query}";
      if (!empty($this->entity->xcxurl)) {
        $url = $this->entity->xcxurl;
      }
      $value = [
        /**
         * @OA\Property(
         *   property="id",
         *   type="integer",
         *   description="ID",
         * ),
         */
        'id' => (int) $this->entity->category_id,
        /**
         * @OA\Property(
         *   property="name",
         *   type="string",
         *   description="显示名称",
         * ),
         */
        'name' => (string) $this->entity->name,
        /**
         * @OA\Property(
         *   property="icon",
         *   type="string",
         *   description="图标URL",
         * ),
         */
        'icon' => $this->imageStyleUrl('CategoryIndexItemIcon', file_load($this->entity->icon)),
        'query' => $query,
        //pages/index/goodslist/goodslist?op=query&query=
        //`goodslist/goodslist?op=query&query=${event.query}`,
        'url' => $url,
      ];
      $this->merge($value);
    }
  }

  protected function getQuery(ShusyIndexCategory $indexCategory) {
    $query = '';
    $category = shusy_category_load($indexCategory->category_id);
    if ($category instanceof ShusyCategory) {
      switch ($category->depth) {
        case 0:
          $query = "depth_1:{$category->depth_1}";
          break;
        case 1:
          $query = "depth_2:{$category->depth_2}";
          break;
        case 2:
          $query = "depth_3:{$category->depth_3}";
          break;
      }
    }
    return $query;
  }
}
